import wechatBot from '@/services/wechatService';
import ernieService from '@/services/ernieService';
import utils from '@/utils/utils';

class ChatController {
  constructor() {
    this.initBot();
  }

  async initBot() {
    try {
      await wechatBot.start();
    } catch (error) {
      console.error('初始化机器人失败:', error);
    }
  }

  async chat(ctx) {
    try {
      const { message } = ctx.request.body;
      const replyContent = await ernieService.chat(message);
      ctx.body = utils.success(replyContent, "Chat completed");
    } catch (error) {
      console.error("Error during chat:", error.message);
      ctx.body = utils.fail("Chat failed: " + error.message);
    }
  }

  async getStatus(ctx) {
    try {
      const status = {
        isRunning: wechatBot.isRunning,
        groupCount: wechatBot.groupConfigs.size,
        conversationCount: wechatBot.conversations.size
      };
      ctx.body = utils.success(status, "Status retrieved");
    } catch (error) {
      ctx.body = utils.fail("Failed to get status: " + error.message);
    }
  }
}

export default new ChatController();